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()); } } }
protected void BeginSend() { if (ToSend.Count == 0) { sendbegined = false; return; } try { SendState state; lock (ToSend) { state = ToSend.Dequeue(); } byte[] buf = Crypt.Encode(PacketFarmer.ToBytes(state.msgType, state.packet)); m_socket.BeginSend(buf, 0, buf.Length, SocketFlags.None, asyncsend, null); } catch (ObjectDisposedException) { // do nothing } catch (SocketException) { Disconnect(); } catch (Exception e) { if (NetConfig.logFilter >= JHSLogFilter.Error) { JHSDebug.LogError("JHSConnection :: Exception: " + e.ToString()); } Disconnect(); } }
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 EndReceive(IAsyncResult result) { if (stage == PerStage.NotConnected) { return; } lock (m_lockread) { try { int len = m_socket.EndReceive(result); if (len == 0) { Disconnect(); return; } byte[] buf = Crypt.Decode(this.m_Connbuffer, len); for (int i = 0; i < buf.Length; i++) { JHSNetworkReader ds = PacketFarmer.Accumulate(buf[i]); if (ds != null) { if (HandleReader(ds)) { if (NetConfig.UseStatistics) { PacketsRec += 1; BitesRev += len; } } } } m_socket.BeginReceive(this.m_Connbuffer, 0, this.m_Connbuffer.Length, SocketFlags.None, asyncrec, null); } catch (ObjectDisposedException) { // do nothing } catch (SocketException sk) { if (NetConfig.logFilter >= JHSLogFilter.Developer) { JHSDebug.LogError("JHSConnection :: Excepiton:" + sk.GetErrorCode()); } Disconnect(); } catch (Exception ex) { if (NetConfig.logFilter >= JHSLogFilter.Developer) { JHSDebug.LogError("JHSConnection :: Excepiton:" + ex.ToString()); } Disconnect(); } } }
internal void InternalSend(short msgType, JHSMessageBase msg) { if (m_activeTransport != null) { m_activeTransport.Send(0, msgType, msg); return; } if (NetConfig.logFilter >= JHSLogFilter.Error) { JHSDebug.LogError("JHSNetworkManager :: Failed to send message to connection ID '" + 0 + ", not found in connection list"); } }
public void Send(uint connectionId, short msgType, JHSMessageBase msg) { if (connection != null && connection.ConnectionReady()) { connection.Send(msgType, msg); return; } if (NetConfig.logFilter >= JHSLogFilter.Error) { JHSDebug.LogError("JHSNetworkManager :: CLIENT BASE TRANSPORT Failed to send message to connection ID '" + connectionId + ", not found in connection list"); } }
public void Write(byte[] buffer, int offset, int count) { if (count > UInt16.MaxValue) { if (NetConfig.logFilter >= JHSLogFilter.Error) { JHSDebug.LogError("JHSNetworkWriter :: Write: buffer is too large (" + count + ") bytes. The maximum buffer size is 64K bytes."); } return; } m_Buffer.WriteBytesAtOffset(buffer, (ushort)offset, (ushort)count); }
public void Send(uint connectionId, short msgType, JHSMessageBase msg) { if (m_Connections.TryGetValue(connectionId, out JHSConnection conection)) { if (conection != null) { conection.Send(msgType, msg); return; } } if (NetConfig.logFilter >= JHSLogFilter.Error) { JHSDebug.LogError("JHSNetworkServer :: Failed to send message to connection ID '" + connectionId + ", not found in connection list"); } }
//NOTE: this will write the entire buffer.. including trailing empty space! public void WriteBytesFull(byte[] buffer) { if (buffer == null) { Write((UInt16)0); return; } if (buffer.Length > UInt16.MaxValue) { if (NetConfig.logFilter >= JHSLogFilter.Error) { JHSDebug.LogError("JHSNetworkWriter :: WriteBytes: buffer is too large (" + buffer.Length + ") bytes. The maximum buffer size is 64K bytes."); } return; } Write((UInt16)buffer.Length); m_Buffer.WriteBytes(buffer, (UInt16)buffer.Length); }
public void WriteBytesAndSize(byte[] buffer, int count) { if (buffer == null || count == 0) { Write((UInt16)0); return; } if (count > UInt16.MaxValue) { if (NetConfig.logFilter >= JHSLogFilter.Error) { JHSDebug.LogError("JHSNetworkWriter :: WriteBytesAndSize: buffer is too large (" + count + ") bytes. The maximum buffer size is 64K bytes."); } return; } Write((UInt16)count); m_Buffer.WriteBytes(buffer, (UInt16)count); }
public void SendToAll(short msgType, JHSMessageBase msg) { try { JHSConnection[] connections = m_Connections.Values.ToArray(); for (int i = 0; i < connections.Length; i++) { if (connections[i] != null && connections[i].ConnectionReady()) { connections[i].Send(msgType, msg); } } } catch (Exception e) { if (NetConfig.logFilter >= JHSLogFilter.Error) { JHSDebug.LogError("JHSNetworkServer :: Exception:" + e.ToString()); } } }
protected bool HandleReader(JHSNetworkReader reader) { try { ushort sz = reader.ReadUInt16(); short msgType = reader.ReadInt16(); byte[] msgBuffer = reader.ReadBytes(sz); if (isClient) { JHSNetworkReader msgReader = new JHSNetworkReader(msgBuffer); if (m_MessageHandlersDict.ContainsKey(msgType)) { JHSNetworkClient.PushMessage(new JHSNetworkMessage() { msgType = msgType, reader = msgReader, conn = this }); } else { if (NetConfig.logFilter >= JHSLogFilter.Error) { JHSDebug.LogError("JHSConnection :: Unknown message ID " + msgType + " connId:" + connectionId); } if (NetConfig.UseStatistics) { ReadError += 1; } } } else { JHSNetworkReader msgReader = new JHSNetworkReader(msgBuffer); JHSNetworkMessageDelegate msgDelegate = null; if (m_MessageHandlersDict.ContainsKey(msgType)) { msgDelegate = m_MessageHandlersDict[msgType]; } if (msgDelegate != null) { msgDelegate(new JHSNetworkMessage() { msgType = msgType, reader = msgReader, conn = this }); } else { if (NetConfig.logFilter >= JHSLogFilter.Error) { JHSDebug.LogError("JHSConnection :: Unknown message ID " + msgType + " connId:" + connectionId); } if (NetConfig.UseStatistics) { ReadError += 1; } } } } catch { } return(true); }