/// <summary> /// Launch Client /// </summary> public void StartAsClient(string ip, int port) { try { skt.BeginConnect(new IPEndPoint(IPAddress.Parse(ip), port), new AsyncCallback(ServerConnectCB), skt); WZTool.LogMsg("\nClient Start Success!\nConnecting To Server......", LogLevel.Info); } catch (Exception e) { WZTool.LogMsg(e.Message, LogLevel.Error); } }
void ServerConnectCB(IAsyncResult ar) { try { skt.EndConnect(ar); session = new T(); session.StartRcvData(skt, null); } catch (Exception e) { WZTool.LogMsg(e.Message, LogLevel.Error); } }
/// <summary> /// Launch Server /// </summary> public void StartAsServer(string ip, int port) { try { skt.Bind(new IPEndPoint(IPAddress.Parse(ip), port)); skt.Listen(backlog); skt.BeginAccept(new AsyncCallback(ClientConnectCB), skt); WZTool.LogMsg("\nServer Start Success!\nWaiting for Connecting......", LogLevel.Info); } catch (Exception e) { WZTool.LogMsg(e.Message, LogLevel.Error); } }
private void SendCB(IAsyncResult ar) { NetworkStream ns = (NetworkStream)ar.AsyncState; try { ns.EndWrite(ar); ns.Flush(); ns.Close(); } catch (Exception e) { WZTool.LogMsg("SndMsgError:" + e.Message, LogLevel.Error); } }
void ClientConnectCB(IAsyncResult ar) { try { Socket clientSkt = skt.EndAccept(ar); T session = new T(); session.StartRcvData(clientSkt, () => { if (sessionLst.Contains(session)) { sessionLst.Remove(session); } }); sessionLst.Add(session); } catch (Exception e) { WZTool.LogMsg(e.Message, LogLevel.Error); } skt.BeginAccept(new AsyncCallback(ClientConnectCB), skt); }
private void RcvBodyData(IAsyncResult ar) { try { WZPkg pack = (WZPkg)ar.AsyncState; int len = skt.EndReceive(ar); if (len > 0) { pack.bodyIndex += len; if (pack.bodyIndex < pack.bodyLen) { skt.BeginReceive(pack.bodyBuff, pack.bodyIndex, pack.bodyLen - pack.bodyIndex, SocketFlags.None, new AsyncCallback(RcvBodyData), pack); } else { T msg = WZTool.DeSerialize <T>(pack.bodyBuff); OnReciveMsg(msg); //loop recive pack.ResetData(); skt.BeginReceive( pack.headBuff, 0, pack.headLen, SocketFlags.None, new AsyncCallback(RcvHeadData), pack); } } else { OnDisConnected(); Clear(); } } catch (Exception e) { WZTool.LogMsg("RcvBodyError:" + e.Message, LogLevel.Error); } }
/// <summary> /// Send binary data /// </summary> public void SendMsg(byte[] data) { NetworkStream ns = null; try { ns = new NetworkStream(skt); if (ns.CanWrite) { ns.BeginWrite( data, 0, data.Length, new AsyncCallback(SendCB), ns); } } catch (Exception e) { WZTool.LogMsg("SndMsgError:" + e.Message, LogLevel.Error); } }
public void StartRcvData(Socket skt, Action closeCB) { try { this.skt = skt; this.closeCB = closeCB; OnConnected(); WZPkg pack = new WZPkg(); skt.BeginReceive( pack.headBuff, 0, pack.headLen, SocketFlags.None, new AsyncCallback(RcvHeadData), pack); } catch (Exception e) { WZTool.LogMsg("StartRcvData:" + e.Message, LogLevel.Error); } }
/// <summary> /// Disconnect network /// </summary> protected virtual void OnDisConnected() { WZTool.LogMsg("Session Disconnected.", LogLevel.Info); }
/// <summary> /// Receive network message /// </summary> protected virtual void OnReciveMsg(T msg) { WZTool.LogMsg("Receive Network Message.", LogLevel.Info); }
/// <summary> /// Connect network /// </summary> protected virtual void OnConnected() { WZTool.LogMsg("New Seesion Connected.", LogLevel.Info); }
/// <summary> /// Send message data /// </summary> public void SendMsg(T msg) { byte[] data = WZTool.PackLenInfo(WZTool.Serialize <T>(msg)); SendMsg(data); }