/// <summary> /// 给指定终端同步发送数据 /// </summary> /// <param name="msg">消息类型</param> /// <param name="data">消息数据正文</param> /// <param name="end">指定终端</param> public void Send(Msg msg, byte[] data, TCPEndPoint end) { byte[] buffer2send = new byte[5 + data.Length]; // 1 + 4 + data BinaryWriter sWriter = new BinaryWriter(new MemoryStream(buffer2send)); sWriter.Write((byte)msg); sWriter.Write(data.Length); sWriter.Write(data); sWriter.Close(); end.Socket.Send(buffer2send); //同步 }
/// <summary> /// 给指定终端异步发送数据 /// </summary> /// <param name="msg">消息类型</param> /// <param name="data">消息数据正文</param> /// <param name="end">指定终端</param> /// <param name="callback">回调方法</param> public void SendAsync(Msg msg, byte[] data, TCPEndPoint end, AsyncCallback callback) { byte[] buffer2send = new byte[5 + data.Length]; // 1 + 4 + data BinaryWriter sWriter = new BinaryWriter(new MemoryStream(buffer2send)); sWriter.Write((byte)msg); sWriter.Write(data.Length); sWriter.Write(data); sWriter.Close(); end.Socket.BeginSend(buffer2send, 0, buffer2send.Length, SocketFlags.None, callback, end); //异步 }
/// <summary> /// 接收数据回调方法 /// </summary> /// <param name="ar">回调参数</param> private void OnReceive(IAsyncResult ar) { TCPEndPoint end = ar.AsyncState as TCPEndPoint; try { int real_recv = end.Socket.EndReceive(ar); end.MStream.Write(end.Buffer, 0, real_recv); //写入消息缓冲区 //尝试读取一条完整消息 ZMessage msg; while (end.MStream.ReadMessage(out msg)) { if ((Msg)msg.head == Msg.Default) //心跳包 跳过 { foreach (KeyValuePair <int, int> p in _pulse_time) { if (p.Key == end.UID) { _pulse_time[p.Key] = _pulse; //重置 break; } } continue; } //处理消息 if (TCPMessageReceived != null) { TCPMessageReceivedEventArgs args = new TCPMessageReceivedEventArgs(); args.Msg = (Msg)msg.head; args.Time = DateTime.Now; args.End = end; args.Data = msg.content; TCPMessageReceived.BeginInvoke(_server_id, args, null, null); //激发事件,通知事件注册者处理消息 } } end.Socket.BeginReceive(end.Buffer, 0, 1024, SocketFlags.None, new AsyncCallback(OnReceive), end); //开始下一次数据接收 } catch { if (TCPClientDisConnected != null) //通知客户端断开 { TCPClientDisConnectedEventArgs args = new TCPClientDisConnectedEventArgs(); args.End = end; args.Time = DateTime.Now; TCPClientDisConnected(_server_id, args); } int tmp; _pulse_time.TryRemove(end.UID, out tmp); } }
/// <summary> /// 连接服务器 /// </summary> /// <param name="ip">服务器IP</param> /// <param name="port">服务器端口</param> public void Connect(string ip, int port) { if (!Connected) { if (_socket == null) { _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } _socket.Connect(new IPEndPoint(IPAddress.Parse(ip), port)); Connected = true; TCPEndPoint end = new TCPEndPoint(); end.Socket = _socket; end.Socket.BeginReceive(end.Buffer, 0, 1024, SocketFlags.None, new AsyncCallback(OnReceive), end); if (TCPClientConnected != null) //通知新客户端连入 { TCPClientConnectedEventArgs args = new TCPClientConnectedEventArgs(); args.End = end; args.Time = DateTime.Now; TCPClientConnected(_client_id, args); } } }
/// <summary> /// 客户端连入回调方法 /// </summary> /// <param name="ar"></param> private void OnAccept(IAsyncResult ar) { try { Socket new_socket = _socket.EndAccept(ar); TCPEndPoint end = new TCPEndPoint(); end.Socket = new_socket; end.UID = _client_index++; end.Socket.BeginReceive(end.Buffer, 0, 1024, SocketFlags.None, new AsyncCallback(OnReceive), end); //开始第一次数据接收 _socket.BeginAccept(new AsyncCallback(OnAccept), null); //开始接受下一次客户端连接 _pulse_time.TryAdd(end.UID, _pulse); //加入心跳检测 if (TCPClientConnected != null) //通知新客户端连入 { TCPClientConnectedEventArgs args = new TCPClientConnectedEventArgs(); args.End = end; args.Time = DateTime.Now; TCPClientConnected(_server_id, args); } } catch { } }
/// <summary> /// 接收数据回调方法 /// </summary> /// <param name="ar"></param> private void OnReceive(IAsyncResult ar) { TCPEndPoint end = ar.AsyncState as TCPEndPoint; try { int real_recv = end.Socket.EndReceive(ar); end.MStream.Write(end.Buffer, 0, real_recv); //写入消息缓冲区 //尝试读取一条完整消息 ZMessage msg; while (end.MStream.ReadMessage(out msg)) { //处理消息 if (TCPMessageReceived != null) { TCPMessageReceivedEventArgs args = new TCPMessageReceivedEventArgs(); args.Msg = (Msg)msg.head; args.Time = DateTime.Now; args.End = end; args.Data = msg.content; TCPMessageReceived.BeginInvoke(_client_id, args, null, null); //激发事件,通知事件注册者处理消息 } } end.Socket.BeginReceive(end.Buffer, 0, 1024, SocketFlags.None, new AsyncCallback(OnReceive), end); } catch { if (TCPClientDisConnected != null) //通知客户端断开 { TCPClientDisConnectedEventArgs args = new TCPClientDisConnectedEventArgs(); args.End = end; args.Time = DateTime.Now; TCPClientDisConnected(_client_id, args); } } }
/// <summary> /// 向指定终端同步发送数据 /// </summary> /// <param name="msg">消息类型</param> /// <param name="data">消息数据正文</param> /// <param name="end">指定终端</param> public void Send(Msg msg, byte[] data, TCPEndPoint end) { TCPServer.TCPServers[_server_id].Send(msg, data, end); }
/// <summary> /// 向指定终端异步发送数据 /// </summary> /// <param name="msg">消息类型</param> /// <param name="data">消息数据正文</param> /// <param name="end">指定终端</param> /// <param name="callback">回调方法</param> public void SendAsync(Msg msg, byte[] data, TCPEndPoint end, AsyncCallback callback) { TCPServer.TCPServers[_server_id].SendAsync(msg, data, end, callback); }