void InitServer() { // wire all the base transports to my events for (int i = 0; i < transports.Length; i++) { // this is required for the handlers, if I use i directly // then all the handlers will use the last i int locali = i; Transport transport = transports[i]; transport.OnServerConnected.AddListener(baseConnectionId => { OnServerConnected.Invoke(FromBaseId(locali, baseConnectionId)); }); transport.OnServerDataReceived.AddListener((baseConnectionId, data) => { OnServerDataReceived.Invoke(FromBaseId(locali, baseConnectionId), data); }); transport.OnServerDataReceivedNonAlloc.AddListener((baseConnectionId, data) => { OnServerDataReceivedNonAlloc.Invoke(FromBaseId(locali, baseConnectionId), data); }); transport.OnServerError.AddListener((baseConnectionId, error) => { OnServerError.Invoke(FromBaseId(locali, baseConnectionId), error); }); transport.OnServerDisconnected.AddListener(baseConnectionId => { OnServerDisconnected.Invoke(FromBaseId(locali, baseConnectionId)); }); } }
public bool ProcessServerMessage() { if (serverHostId == -1) { return(false); } int connectionId = -1; NetworkEventType networkEvent = NetworkTransport.ReceiveFromHost(serverHostId, out connectionId, out int channel, serverReceiveBuffer, serverReceiveBuffer.Length, out int receivedSize, out error); // note: 'error' is used for extra information, e.g. the reason for // a disconnect. we don't necessarily have to throw an error if // error != 0. but let's log it for easier debugging. // // DO NOT return after error != 0. otherwise Disconnect won't be // registered. NetworkError networkError = (NetworkError)error; if (networkError != NetworkError.Ok) { string message = "NetworkTransport.Receive failed: hostid=" + serverHostId + " connId=" + connectionId + " channelId=" + channel + " error=" + networkError; // TODO write a TransportException or better OnServerError.Invoke(connectionId, new Exception(message)); } // LLAPI client sends keep alive messages (75-6C-6C) on channel=110. // ignore all messages that aren't for our selected channel. /*if (channel != channelId) * { * return false; * }*/ switch (networkEvent) { case NetworkEventType.ConnectEvent: OnServerConnected.Invoke(connectionId); break; case NetworkEventType.DataEvent: OnServerDataReceivedNonAlloc.Invoke(connectionId, new ArraySegment <byte>(serverReceiveBuffer, 0, receivedSize)); break; case NetworkEventType.DisconnectEvent: OnServerDisconnected.Invoke(connectionId); break; default: // nothing or a message we don't recognize return(false); } return(true); }