public RPCServer(BaseSocket socket) : base(socket) { _handler.Add(typeof(GetMemberRequest), _handler_GetMember); _handler.Add(typeof(SetMemberRequest), _handler_SetMember); _handler.Add(typeof(InvokeRequest), _handler_Invoke); _handler.Add(typeof(ProxyRequest), _handler_Proxy); }
public RPCClient(BaseSocket socket) : base(socket) { _handler.Add(typeof(ExceptionResponse), _handler_Exception); _handler.Add(typeof(GetMemberResponse), _handler_GetMember); _handler.Add(typeof(SetMemberResponse), _handler_GetMember); _handler.Add(typeof(InvokeResponse), _handler_Invoke); _handler.Add(typeof(ProxyResponse), _handler_Proxy); }
void _socket_Closed(BaseSocket sender) { if (this.Closed != null) this.Closed(this); }
void _socket_DataArrived(BaseSocket sender, byte[] data) { _msgdata.Position = _msgdata.Length; _msgdata.Write(data, 0, data.Length); _msgdata.Position = 0; BasePacket message = null; do { message = null; if (_msgdata.Length >= 4 && _msglen == 0) { _msglen = ByteWriter.ReadInt32_BE(_msgdata.GetBuffer(), 0); _msgdata.Position = 4; _msgdata = _msgdata.Cut(); } if (_msgdata.Length >= _msglen && _msglen > 0) { message = BasePacket.Deserialize(_msgdata.ToArray().Slice(0, _msglen)); if (_msgdata.Length == _msglen) _msgdata = new MemoryStream(); else { _msgdata.Position = _msglen; _msgdata = _msgdata.Cut(); } _msglen = 0; } #if (COMMTEST) Console.WriteLine(string.Format("{0} ~ ms[{1}]", IsServer ? "Server" : "Client", _msgdata.Length)); #endif if (message != null) { #if (COMMTEST) Console.WriteLine(message); #endif try { Type msgType = message.GetType(); foreach (var handler in _handler) { if (handler.Key == msgType) { BaseResponse resp = handler.Value(message); if (resp != null) this.Send(resp); break; } } } catch (Exception ex) { this.Send(new ExceptionResponse(ex.ToString(), message)); } } } while (message != null); }
public RPCBase(BaseSocket socket) { _socket = socket; _socket.DataArrived += new BaseSocket.DataArrivedHandler(_socket_DataArrived); _socket.Closed += new BaseSocket.ClosedHandler(_socket_Closed); }