protobuf-swift icon indicating copy to clipboard operation
protobuf-swift copied to clipboard

gRPC support

Open alexeyxo opened this issue 9 years ago • 20 comments

alexeyxo avatar Jul 16 '15 10:07 alexeyxo

I'm curious if you're going to write a gRPC server implementation in Swift or only writing the code generator? I'm interested in developing such a server and found your repo.

tengyifei avatar Dec 30 '15 06:12 tengyifei

@tengyifei: I am currently working on a gRPC server/client framework for Swift, based on the work by @alexeyxo. I am only just starting but I wonder if you have already made any progress on this?

bryanpkc avatar Jul 08 '16 20:07 bryanpkc

Not yet. But I'd be happy to work with you. We could wrap the C core API provided by gRPC, just like the objective-c client.

tengyifei avatar Jul 09 '16 06:07 tengyifei

@tengyifei Sounds good. That's what I'm doing now. I'll push some code to GitHub once it compiles.

bryanpkc avatar Jul 11 '16 14:07 bryanpkc

@bryanpkc We should PM each other and stop blasting this thread. I'm on Telegram as gjftw. Side note: Working at Google as an intern on the user-facing C API for gRPC right now ;)

tengyifei avatar Jul 11 '16 21:07 tengyifei

I currently work above gRPC, but I have many problems with swift 3.0. I think gRPC need to develop for 3.0 version of Swift. I will open the code after 2 weeks.

alexeyxo avatar Jul 12 '16 09:07 alexeyxo

@alexeyxo: I can help with testing and patching the code with Swift 3.0. I have been looking at the Objective-C implementation of gRPC and trying to rewrite it into Swift 3.0, but it's taking a bit of time, and I don't want to duplicate effort if you already have some code written.

P.S. I currently have a ProtoBuf3.0-Swift3.0 + gRPC branch that compiles cleanly and works for simple message serialization cases.

bryanpkc avatar Jul 12 '16 16:07 bryanpkc

@bryanpkc Du you use gRPC-obj-c implementation for sending data, or Swift+gRPC(с++) library? I have many problems by adding c++ to Swift. How did you do?

alexeyxo avatar Jul 14 '16 16:07 alexeyxo

@alexeyxo Not recommending wrapping C++ in Swift. The C++ implementation wraps the C core and is way too complex. I believe in writing Swift implementation from scratch that also wraps the C core, at the same time using Swift-idiomatic constructs. This way we are using neither the Objective-C nor the C++ implementation.

tengyifei avatar Jul 14 '16 20:07 tengyifei

Sorry, I mean "C". I can't to connect C library in swift-project. My compiler creates services and methods, but framework does not work. I made "module.map", but i have error:

"/usr/local/Cellar/grpc/0.15.0/include/grpc/impl/codegen/time.h:98:21: Conflicting types for 'gpr_time_max'"

alexeyxo avatar Jul 14 '16 20:07 alexeyxo

@alexeyxo I am wrapping around the C core, by basically translating the Objective-C implementation into Swift. I am new to both Objective-C and gRPC, and this translation is taking longer than I hoped. I am also having problems with module maps; gRPC headers that include pthread.h is causing swiftc to barf with errors like this:

/opt/grpc/include/grpc/impl/codegen/sync_posix.h:40:10: note: in file included from /opt/grpc/include/grpc/impl/codegen/sync_posix.h:40:
#include <bits/pthreadtypes.h>
         ^
/usr/include/x86_64-linux-gnu/bits/pthreadtypes.h:92:10: error: redefinition of '__pthread_mutex_s'
  struct __pthread_mutex_s
         ^
/usr/include/x86_64-linux-gnu/bits/pthreadtypes.h:92:10: note: previous definition is here
  struct __pthread_mutex_s

bryanpkc avatar Jul 18 '16 10:07 bryanpkc

Can you update your copy of gRPC core and see if that works? I'm basing my work directly on top of gRPC master and my sync_posix.h only references pthread.h.

#ifndef GRPC_IMPL_CODEGEN_SYNC_POSIX_H
#define GRPC_IMPL_CODEGEN_SYNC_POSIX_H

#include <grpc/impl/codegen/sync_generic.h>

#include <pthread.h>

typedef pthread_mutex_t gpr_mu;
typedef pthread_cond_t gpr_cv;
typedef pthread_once_t gpr_once;

#define GPR_ONCE_INIT PTHREAD_ONCE_INIT

#endif /* GRPC_IMPL_CODEGEN_SYNC_POSIX_H */

tengyifei avatar Jul 18 '16 19:07 tengyifei

@alexeyxo The problem you're seeing may also be caused by an outdated version of gRPC. The core is evolving very quickly and no guarantee of core API compatibility exists as of now.

tengyifei avatar Jul 18 '16 19:07 tengyifei

@tengyifei Yes, sync_posix.h only uses <pthread.h> but it was causing issues too, so I tried replacing it with <bits/pthreadtypes.h>:

/opt/grpc/include/grpc/impl/codegen/sync_posix.h:41:9: error: declaration of 'pthread_mutex_t' must be imported from module 'SwiftGlibc.C.signal' before it is required
typedef pthread_mutex_t gpr_mu;
        ^
/usr/include/x86_64-linux-gnu/bits/pthreadtypes.h:128:3: note: previous declaration is here
} pthread_mutex_t;

It seems that C libraries that use libpthread are not as simple to import into Swift, as the pthread symbols will clash with Swift's own Glibc module. I am going to try debugging the tool chain to understand why it's behaving this way and whether there is a workaround.

bryanpkc avatar Jul 19 '16 23:07 bryanpkc

fyi - I built this docker file which borrows from other github projects to expose Parsey Mcparseface + grpc on port 9000 https://github.com/johndpope/DockerParseyMcParsefaceAPI

There's a simple node js client with about 10 lines of code https://github.com/johndpope/DockerParseyMcParsefaceAPI/blob/master/clients/node_client/index.js ` var grpc = require('grpc');

var protoDescriptor = grpc.load({root: __dirname+'/api', file:'cali/nlp/parsey_api.proto'});

var service = new protoDescriptor.cali.nlp.ParseyService("0.0.0.0:9000", grpc.credentials.createInsecure());

service.parse(["This is the first sentence", "I love this sentence"], function(err, response) { console.log(JSON.stringify(response,null,' ')); });`

I have a separate docker container for running swift3 server and want to connect. But it's not so clear best way to do this. The protoc models are easy to build.... https://github.com/johndpope/DockerParseyMcParsefaceAPI/tree/master/clients/ios_client/proto

Did anyone above - get anywhere with this? or is this project somewhat neglected due to Apple introducing their own protobuf code?

johndpope avatar Oct 22 '16 15:10 johndpope

Ah - it looks like Google has come through (for ios) side. https://github.com/grpc/grpc/blob/master/src/objective-c/examples/SwiftSample/ViewController.swift

johndpope avatar Oct 22 '16 15:10 johndpope

fyi - swift server side grpc + SwiftProtobuf https://github.com/nathanborror/swift-grpc/blob/master/Sources/gRPC.swift

threw this together https://github.com/johndpope/swift3-grpc-example Seems to be missing headers Missing :authority header https://github.com/johndpope/DockerParseyMcParsefaceAPI/issues/2

johndpope avatar Oct 22 '16 15:10 johndpope

@johndpope This project live and will be live. Currently, I haven't time doing this project full time, but in the future, I believe, all will be changed.

alexeyxo avatar Oct 25 '16 15:10 alexeyxo

Good news - swift3 -server side support. Thanks Huawei

https://github.com/Huawei-PTLab/SwiftGRPC https://github.com/Huawei-PTLab/SwiftGRPCExamples

johndpope avatar Oct 25 '16 16:10 johndpope

Hi guys, @TimBurks and I would love it if we all joined efforts at https://github.com/grpc/grpc-swift :)

jcanizales avatar Oct 26 '16 01:10 jcanizales