internal void RegisterHandlerSafe(short msgType, JHSNetworkMessageDelegate handler) { if (handler == null) { if (NetConfig.logFilter >= JHSLogFilter.Error) { JHSDebug.LogError("RegisterHandlerSafe id:" + msgType + " handler is null"); } return; } if (NetConfig.logFilter >= JHSLogFilter.Developer) { JHSDebug.Log("RegisterHandlerSafe id:" + msgType + " handler:" + handler.GetMethodName()); } if (m_MsgHandlers.ContainsKey(msgType)) { if (NetConfig.logFilter >= JHSLogFilter.Error) { JHSDebug.LogError("RegisterHandlerSafe id:" + msgType + " handler:" + handler.GetMethodName() + " conflict"); } return; } m_MsgHandlers.Add(msgType, handler); }
private void EndSend(IAsyncResult ar) { try { int bytesSent = m_socket.EndSend(ar); if (NetConfig.UseStatistics) { BytesSent += bytesSent; PacketsSend += 1; lastSentTime = JHSTime.Time; } BeginSend(); if (NetConfig.logFilter >= JHSLogFilter.Developer) { JHSDebug.Log(string.Format("JHSConnection :: Sent {0} bytes.", bytesSent)); } } catch (ObjectDisposedException) { // do nothing } catch (SocketException) { Disconnect(); } catch (Exception e) { if (NetConfig.logFilter >= JHSLogFilter.Error) { JHSDebug.LogError("JHSConnection :: Exception: " + e.ToString()); } Disconnect(); } }
public void StartListening() { IPAddress ipAddress = IPAddress.Parse(NetConfig.IP); IPEndPoint localEndPoint = new IPEndPoint(ipAddress, NetConfig.Port); _receiveSocket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); try { _receiveSocket.Bind(localEndPoint); _receiveSocket.Listen(100); if (NetConfig.logFilter >= JHSLogFilter.Developer) { JHSDebug.Log("JHSNetworkServer :: Started to listen :" + ipAddress.ToString() + " Port:" + NetConfig.Port + " Protocol Version:" + NetConfig.Version); } else { JHSDebug.Log("Server Started IP[" + ipAddress.ToString() + ":" + NetConfig.Port + "] Version:[" + NetConfig.Version + "]"); } _receiveSocket.BeginAccept(new AsyncCallback(AcceptCallback), _receiveSocket); } catch (Exception e) { if (NetConfig.logFilter >= JHSLogFilter.Developer) { JHSDebug.LogError("JHSNetworkServer :: Excepiton:" + e.ToString()); } } }
public void DoUpdate() { if (Connecting) { return; } if (PermaDisconnected) { return; } if (!Connected) { if (LastTryToConnect > JHSTime.Time) { return; } if (RecconectTry > m_RecconectTry) { LastTryToConnect = JHSTime.Time + NetConfig.ReconnectTimeOut; m_RecconectTry++; StartClient(); } else { if (NetConfig.logFilter >= JHSLogFilter.Developer) { JHSDebug.Log("JHSProtocol Could not connect to the server."); } PermaDisconnected = true; } } }
public void StartConnect() { IPAddress ipAddress = IPAddress.Parse(IP); remoteEP = new IPEndPoint(ipAddress, Port); if (connection != null && !connection.m_Disposed) { connection.Dispose(); connection = null; } connection = new JHSConnection(); connection.Init(true); if (NetConfig.logFilter >= JHSLogFilter.Developer) { JHSDebug.Log("JHSNetworkManager Connecting to Server reconnect attmpt:" + m_RecconectTry); } connection.m_socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp) { Blocking = false }; connection.m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 1048576); connection.m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, 1048576); connection.m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, 1); connection.m_socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, 0); connection.m_socket.BeginConnect(remoteEP, ConnectCallback, connection.m_socket); Connecting = true; PermaDisconnected = false; }
internal void AddConnection(JHSConnection con) { Connected(con); if (NetConfig.logFilter >= JHSLogFilter.Developer) { JHSDebug.Log("JHSNetworkManager :: Added Connection To Pool."); } }
private void ConnectCallback(IAsyncResult ar) { Socket client = (Socket)ar.AsyncState; try { client.EndConnect(ar); } catch (SocketException x) { if (connection != null) { connection.Disconnect(); } if (NetConfig.logFilter >= JHSLogFilter.Developer) { JHSDebug.Log("JHSNetworkManager :: Excepiton:" + x.GetErrorCode()); } Connecting = false; return; } catch (Exception e) { if (connection != null) { connection.Disconnect(); } if (NetConfig.logFilter >= JHSLogFilter.Developer) { JHSDebug.Log("JHSNetworkManager :: Excepiton:" + e.ToString()); } Connecting = false; return; } try { connection.SetHandlers(m_MessageHandlers); connection.StartReceiving(client); HandShakeMsg packet = new HandShakeMsg { Version = NetConfig.Version, OP = 0, }; connection.Send(InternalMessages.HeandShake_Server, packet); Connecting = false; Connected = true; m_RecconectTry = 0; } catch (Exception e) { if (NetConfig.logFilter >= JHSLogFilter.Developer) { JHSDebug.Log("JHSNetworkManager :: Excepiton:" + e.ToString()); } Connecting = false; } }
internal void Discconnectx(JHSConnection con) { if (m_activeTransport != null) { m_activeTransport.Disconnect(con); if (NetConfig.logFilter >= JHSLogFilter.Log && con != null) { JHSDebug.Log("JHSNetworkManager :: Disconnected :" + con.connectionId); } } }
public JHSConnection StartClient() { if (NetConfig.logFilter >= JHSLogFilter.Log) { JHSDebug.Log("JHSNetworkManager :: Created Client Version:" + NetConfig.Version); } Connecting = true; IP = NetConfig.IP; Port = NetConfig.Port; StartConnect(); Connected = false; return(null); }
public byte[] ToBytes(short msgType, JHSMessageBase packet) { lock (m_Writer) { m_Writer.StartMessage(msgType); packet.Serialize(m_Writer); m_Writer.FinishMessage(); byte[] buf = m_Writer.ToArray(); List <byte> listbuf = new List <byte>(); listbuf.AddRange(Write(PACKAGE_HEADER_ID)); listbuf.AddRange(Write((ushort)buf.Length)); listbuf.AddRange(buf); byte[] nesent = listbuf.ToArray(); if (NetConfig.logFilter >= JHSLogFilter.Developer) { JHSDebug.Log("JHSPacketFarmer Write:" + BitConverter.ToString(nesent)); } return(nesent); } }
private static void TESTMSG_RECIVE(JHSNetworkMessage netMsg) { uint connectionId = netMsg.conn.connectionId; string ip = netMsg.conn.IP; SearchMatch packet = netMsg.ReadMessage <SearchMatch>(); if (packet != null) { JHSDebug.Log(packet.op.ToString()); netMsg.conn.Send(100, new SearchMatch() { op = SearchMatchOperations.Search }); netMsg.conn.Send(100, new SearchMatch() { op = SearchMatchOperations.START, IP = "127.0.0.1", port = 1985 }); // JHSNetworkManager.SendToAll(100, new TESTMSGRE() {ClientId = connectionId, Time = packet.Time, TimeServ = JHSTime.TimeStamp }); } }
public JHSNetworkReader Accumulate(byte bt) { btbuf.Add(bt); if (btbuf.Count < PACKAGE_HEADER_LEN) { return(null); } byte[] buff = btbuf.ToArray(); uint head = ReadUInt32(buff, 0); uint len = ReadUInt16(buff, 4); int ptr = btbuf.Count - PACKAGE_HEADER_LEN; if (head == PACKAGE_HEADER_ID) { if (btbuf.Count >= 65536) { btbuf.Clear(); return(null); } if (len == ptr) { if (NetConfig.logFilter >= JHSLogFilter.Developer) { JHSDebug.Log("JHSPacketFarmer Read:" + BitConverter.ToString(buff)); } btbuf.Clear(); var x = new JHSNetworkReader(buff); x.ReadBytes(PACKAGE_HEADER_LEN); return(x); } } return(null); }
internal void HandleServerHandShake(JHSNetworkMessage netMsg) { HandShakeMsg packet = netMsg.ReadMessage <HandShakeMsg>(); if (packet != null) { if (packet.OP == 0) //VER VERSION { if (packet.Version == NetConfig.Version) { netMsg.conn.stage = PerStage.Verifying; HandShakeMsg p = new HandShakeMsg { Version = netMsg.conn.connectionId, OP = 0 }; netMsg.conn.Send(InternalMessages.HeandShake_Client, p); } else { netMsg.conn.Disconnect(); } } else if (packet.OP == 1) { if (packet.Version == NetConfig.Key) { netMsg.conn.stage = PerStage.Connecting; HandShakeMsg p = new HandShakeMsg { Version = (uint)NetConfig.Key, OP = 1 }; netMsg.conn.Send(InternalMessages.HeandShake_Client, p); } else { netMsg.conn.Disconnect(); } } else if (packet.OP == 2) { if (packet.Version == NetConfig.Key) { netMsg.conn.stage = PerStage.Connected; if (NetConfig.logFilter >= JHSLogFilter.Log) { JHSDebug.Log("JHSNetworkManager :: Connected:" + netMsg.conn.connectionId); } AddConnection(netMsg.conn); } else { netMsg.conn.Disconnect(); } } else { netMsg.conn.Disconnect(); } } }