public UnityConnection() { // register routes for protorouter ProtoRouter.registerRoute(DataWrapper.MsgOneofCase.DataList, new UnityDataRoute()); ProtoRouter.registerRoute(DataWrapper.MsgOneofCase.UnitySettings, new UnityClientListRoute()); ProtoRouter.registerRoute(DataWrapper.MsgOneofCase.CommandChange, new CommandHolderRoute()); ProtoRouter.registerRoute(DataWrapper.MsgOneofCase.GetCommand, new CommandBufferRoute()); TcpListener server = new TcpListener(netConfig.Address, netConfig.Port); try { server.Start(); while (true) { var handler = new SocketHandler(server.AcceptTcpClient()); new Thread(new ThreadStart(handler.HandleConnection)).Start(); } } catch (Exception e) { Console.WriteLine("connect exception " + e, true); } finally { server.Stop(); } }
public void HandleConnection() { clientID = ProtoRouter.clientConnected(); //get a unique id and initialize routes try { do // do while (client.Connected); { DataWrapper wrapper = new DataWrapper(); // create wrapper protobuf var stream = client.GetStream(); // get stream do // do while (stream.DataAvailable); { wrapper.MergeDelimitedFrom(stream); // merge data }while (stream.DataAvailable); // end do while (stream.DataAvailable); ProtoRouter.routeProtobuf(wrapper, clientID, this); // route data stream.Flush(); // empty stream } while (client.Connected); // end do while (client.Connected); } catch (Exception e) { Console.WriteLine("Exception: {0}", e); } // catch ProtoRouter.clientEndConnection(clientID); // tell routes to remove client client.Close(); }
//====================================================================================== // Purpose: rout for unityInfo protobuf updates UnityClientList // ===================================================================================== public override void route(DataWrapper wrapper, string id, SocketHandler sh) { if (!wrapper.UnitySettings.Name.Equals(id)) { string newID = UnityClientList.registerClient(wrapper.UnitySettings.Name); Console.WriteLine($"UCLR: Name Change {id} to {newID}"); ProtoRouter.clientNameChange(id, newID); UnityClientList.removeClient(id); sh.clientID = newID; id = newID; DataWrapper returnWrapper = new DataWrapper { UnitySettings = new UnitySettings { Name = newID } }; sh.sendToServer(returnWrapper); } if (UnityClientList.clientExists(id)) { Console.WriteLine($"UCLR: client {id} state set to {wrapper.UnitySettings.Public}"); UnityClientList.setClientPublic(id, wrapper.UnitySettings.Public); } }