123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using HPSocketCS;
- namespace SSLServerNS
- {
- public enum AppState
- {
- Starting, Started, Stoping, Stoped, Error
- }
- public partial class frmServer : Form
- {
- private AppState appState = AppState.Stoped;
- private delegate void ShowMsg(string msg);
- private ShowMsg AddMsgDelegate;
- HPSocketCS.TcpPackServer server = new HPSocketCS.TcpPackServer();
-
- private string title = "Echo TcpPackServer [ 'C' - clear list box ]";
- public frmServer()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- try
- {
- this.Text = title;
- // 本机测试没必要改地址,有需求请注释或删除
- this.txtIpAddress.ReadOnly = true;
- // 加个委托显示msg,因为on系列都是在工作线程中调用的,ui不允许直接操作
- AddMsgDelegate = new ShowMsg(AddMsg);
- // 设置服务器事件
- server.OnPrepareListen += new TcpServerEvent.OnPrepareListenEventHandler(OnPrepareListen);
- server.OnAccept += new TcpServerEvent.OnAcceptEventHandler(OnAccept);
- server.OnSend += new TcpServerEvent.OnSendEventHandler(OnSend);
- server.OnReceive += new TcpServerEvent.OnReceiveEventHandler(OnReceive);
- server.OnClose += new TcpServerEvent.OnCloseEventHandler(OnClose);
- server.OnShutdown += new TcpServerEvent.OnShutdownEventHandler(OnShutdown);
- // 设置包头标识,与对端设置保证一致性
- server.PackHeaderFlag = 0xff;
- // 设置最大封包大小
- server.MaxPackSize = 0x1000;
- SetAppState(AppState.Stoped);
- }
- catch (Exception ex)
- {
- SetAppState(AppState.Error);
- AddMsg(ex.Message);
- }
- }
- private void btnStart_Click(object sender, EventArgs e)
- {
- try
- {
- String ip = this.txtIpAddress.Text.Trim();
- ushort port = ushort.Parse(this.txtPort.Text.Trim());
- // 写在这个位置是上面可能会异常
- SetAppState(AppState.Starting);
- server.IpAddress = ip;
- server.Port = port;
- // 启动服务
- if (server.Start())
- {
- this.Text = string.Format("{2} - ({0}:{1})", ip, port, title);
- SetAppState(AppState.Started);
- AddMsg(string.Format("$Server Start OK -> ({0}:{1})", ip, port));
- }
- else
- {
- SetAppState(AppState.Stoped);
- throw new Exception(string.Format("$Server Start Error -> {0}({1})", server.ErrorMessage, server.ErrorCode));
- }
- }
- catch (Exception ex)
- {
- AddMsg(ex.Message);
- }
- }
- private void btnStop_Click(object sender, EventArgs e)
- {
- SetAppState(AppState.Stoping);
- // 停止服务
- AddMsg("$Server Stop");
- if (server.Stop())
- {
- this.Text = title;
- SetAppState(AppState.Stoped);
- }
- else
- {
- AddMsg(string.Format("$Stop Error -> {0}({1})", server.ErrorMessage, server.ErrorCode));
- }
- }
- private void btnDisconn_Click(object sender, EventArgs e)
- {
- try
- {
- IntPtr connId = (IntPtr)Convert.ToInt32(this.txtDisConn.Text.Trim());
- // 断开指定客户
- if (server.Disconnect(connId, true))
- {
- AddMsg(string.Format("$({0}) Disconnect OK", connId));
- }
- else
- {
- throw new Exception(string.Format("Disconnect({0}) Error", connId));
- }
- }
- catch (Exception ex)
- {
- AddMsg(ex.Message);
- }
- }
- HandleResult OnPrepareListen(IntPtr soListen)
- {
- // 监听事件到达了,一般没什么用吧?
- return HandleResult.Ok;
- }
- HandleResult OnAccept(IntPtr connId, IntPtr pClient)
- {
- // 客户进入了
- // 获取客户端ip和端口
- string ip = string.Empty;
- ushort port = 0;
- if (server.GetRemoteAddress(connId, ref ip, ref port))
- {
- AddMsg(string.Format(" > [{0},OnAccept] -> PASS({1}:{2})", connId, ip.ToString(), port));
- }
- else
- {
- AddMsg(string.Format(" > [{0},OnAccept] -> Server_GetClientAddress() Error", connId));
- }
- // 设置附加数据
- ClientInfo clientInfo = new ClientInfo();
- clientInfo.ConnId = connId;
- clientInfo.IpAddress = ip;
- clientInfo.Port = port;
- if (server.SetExtra(connId, clientInfo) == false)
- {
- AddMsg(string.Format(" > [{0},OnAccept] -> SetConnectionExtra fail", connId));
- }
- return HandleResult.Ok;
- }
- HandleResult OnSend(IntPtr connId, byte[] bytes)
- {
- // 服务器发数据了
- AddMsg(string.Format(" > [{0},OnSend] -> ({1} bytes)", connId, bytes.Length));
- return HandleResult.Ok;
- }
- HandleResult OnReceive(IntPtr connId, byte[] bytes)
- {
- // 数据到达了
- try
- {
- // 获取附加数据
- var clientInfo = server.GetExtra<ClientInfo>(connId);
- if (clientInfo != null)
- {
- // clientInfo 就是accept里传入的附加数据了
- AddMsg(string.Format(" > [{0},OnReceive] -> {1}:{2} ({3} bytes)", clientInfo.ConnId, clientInfo.IpAddress, clientInfo.Port, bytes.Length));
- }
- else
- {
- AddMsg(string.Format(" > [{0},OnReceive] -> ({1} bytes)", connId, bytes.Length));
- }
- if (server.Send(connId, bytes, bytes.Length))
- {
- return HandleResult.Ok;
- }
- return HandleResult.Error;
- }
- catch (Exception)
- {
- return HandleResult.Ignore;
- }
- }
- HandleResult OnClose(IntPtr connId, SocketOperation enOperation, int errorCode)
- {
- if(errorCode == 0)
- AddMsg(string.Format(" > [{0},OnClose]", connId));
- else
- AddMsg(string.Format(" > [{0},OnError] -> OP:{1},CODE:{2}", connId, enOperation, errorCode));
- // return HPSocketSdk.HandleResult.Ok;
- if (server.RemoveExtra(connId) == false)
- {
- AddMsg(string.Format(" > [{0},OnClose] -> SetConnectionExtra({0}, null) fail", connId));
- }
- return HandleResult.Ok;
- }
- HandleResult OnShutdown()
- {
- // 服务关闭了
- AddMsg(" > [OnShutdown]");
- return HandleResult.Ok;
- }
- /// <summary>
- /// 设置程序状态
- /// </summary>
- /// <param name="state"></param>
- void SetAppState(AppState state)
- {
- appState = state;
- this.btnStart.Enabled = (appState == AppState.Stoped);
- this.btnStop.Enabled = (appState == AppState.Started);
- this.txtIpAddress.Enabled = (appState == AppState.Stoped);
- this.txtPort.Enabled = (appState == AppState.Stoped);
- this.txtDisConn.Enabled = (appState == AppState.Started);
- this.btnDisconn.Enabled = (appState == AppState.Started && this.txtDisConn.Text.Length > 0);
- }
- /// <summary>
- /// 往listbox加一条项目
- /// </summary>
- /// <param name="msg"></param>
- void AddMsg(string msg)
- {
- if (this.lbxMsg.InvokeRequired)
- {
- // 很帅的调自己
- this.lbxMsg.Invoke(AddMsgDelegate, msg);
- }
- else
- {
- if (this.lbxMsg.Items.Count > 100)
- {
- this.lbxMsg.Items.RemoveAt(0);
- }
- this.lbxMsg.Items.Add(msg);
- this.lbxMsg.TopIndex = this.lbxMsg.Items.Count - (int)(this.lbxMsg.Height / this.lbxMsg.ItemHeight);
- }
- }
- private void txtDisConn_TextChanged(object sender, EventArgs e)
- {
- // CONNID框被改变事件
- this.btnDisconn.Enabled = (appState == AppState.Started && this.txtDisConn.Text.Length > 0);
- }
- private void lbxMsg_KeyPress(object sender, KeyPressEventArgs e)
- {
- // 清理listbox
- if (e.KeyChar == 'c' || e.KeyChar == 'C')
- {
- this.lbxMsg.Items.Clear();
- }
- }
- private void frmServer_FormClosed(object sender, FormClosedEventArgs e)
- {
- server.Destroy();
- }
- }
- [StructLayout(LayoutKind.Sequential)]
- public class ClientInfo
- {
- public IntPtr ConnId { get; set; }
- public string IpAddress { get; set; }
- public ushort Port { get; set; }
- }
- }
|